package org.itboys.product.service;

import javax.annotation.Resource;

import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.entity.ProductCategorySpecDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 规格特征详情
 * @author weisky
 *
 */
@Service
public class ProductCategorySpecDetailService extends BaseService<ProductCategorySpecDetail, Long>{

	private static final long serialVersionUID = 3598337692630171164L;
	
	@Resource(name = "productDS")
	private MongoDataSource ds;
	@Autowired
	private ResourceHolder resourceHolder;
	
	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<ProductCategorySpecDetail> getEntityClass() {
		return ProductCategorySpecDetail.class;
	}

	public boolean insert(ProductCategorySpecDetail productCategorySpecDetail, MultipartFile tImage){
		try{
			
			ProductCategorySpecDetail pc = null;
			if(productCategorySpecDetail.getId() != 0){
				pc = getById(productCategorySpecDetail.getId());
			}
			
			if(tImage != null && !tImage.isEmpty() && tImage.getSize() > 0){
				String tImagePath = FileUtils.saveFile(tImage.getInputStream(), 
						resourceHolder.getStringValue("fileUploadPath"), tImage.getOriginalFilename(), tImage.getContentType());
				productCategorySpecDetail.setImage(tImagePath);
			}
			
			if(productCategorySpecDetail.getId() != 0){
				productCategorySpecDetail.setImage(pc.getImage());
				update(productCategorySpecDetail);
			}else{
				save(productCategorySpecDetail);
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ProductCategorySpecDetailService.insert error", e);
		}
		return false;
	}
	
	public boolean dele(Long id){
		try{
			delete(id);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ProductCategorySpecDetailService.dele error", e);
		}
		return false;
	}
	
}
